using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Microsoft.Practices.WizardFramework;
using VSSDKAssist.Assets.Converters;
using VSSDKAssist.Recipes.Common.Editors;
using VSSDKAssist.Recipes.Common.Converters;
using EnvDTE;
using VSSDKAssist.Assets.Editors;
using System.Collections.Specialized;
using Microsoft.Practices.RecipeFramework.Library.Converters;
using Microsoft.Practices.RecipeFramework.Library.Editors;
using VSSDKAssist.Recipes.Common.CustomPages;
using System.Globalization;
using VSSDKAssist.Recipes.Common.Helpers;

namespace VSSDKAssist.Recipes.CreateVSCommandVSCT
{
	/// <summary>
	/// Custom page for CreateVSCommand recipe
	/// </summary>
	public partial class CreateVSCommandCP2 : WizardPageBase
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="CreateVSCommandCP2"/> class.
		/// </summary>
		public CreateVSCommandCP2()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CreateVSCommandCP2"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
		public CreateVSCommandCP2(WizardForm wizard)
			: base(wizard)
		{
			InitializeComponent();
			SetControls();
		}

		/// <summary>
		/// Gets or sets the command caption.
		/// </summary>
		/// <value>The command caption.</value>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value"), RecipeArgument]
		public string CommandCaption
		{
			get { return veCommandCaption.Value.ToString(); }
			set
			{
			}
		}

		/// <summary>
		/// Gets or sets the parent group id alias.
		/// </summary>
		/// <value>The parent group id alias.</value>
		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "value"), RecipeArgument]
		public string ParentGroupIdAlias
		{
			get { return veParentGroupIdAlias.Value.ToString(); }
			set
			{
			}
		}

		/// <summary>
		/// Gets or sets a value indicating whether [default parent group].
		/// </summary>
		/// <value><c>true</c> if [default parent group]; otherwise, <c>false</c>.</value>
		[RecipeArgument]
		public bool DefaultParentGroup
		{
			get { return !chkOther.Checked; }
			set
			{
				chkOther.Checked = !value;
			}
		}

		/// <summary>
		/// Gets or sets the other parent group GUID.
		/// </summary>
		/// <value>The other parent group GUID.</value>
		[RecipeArgument]
		public string OtherParentGroupGuid
		{
			get { return veCommandGuid.Value.ToString(); }
			set
			{
				if(value != null)
				{
					veCommandGuid.TextBox.Text = value.ToString();
				}
			}
		}

		/// <summary>
		/// Gets or sets the other parent group id.
		/// </summary>
		/// <value>The other parent group id.</value>
		[RecipeArgument]
		public string OtherParentGroupId
		{
			get { return (string)veCommandId.Value; }
			set
			{
				if(value != null)
				{
					veCommandId.TextBox.Text = value.ToString();
				}
			}
		}

		/// <summary>
		/// Gets or sets the name of the bitmap file.
		/// </summary>
		/// <value>The name of the bitmap file.</value>
		[RecipeArgument]
		public string BitmapFileName
		{
			get { return (string)veCommandIcon.Value; }
			set
			{
				if(value != null)
				{
					veCommandIcon.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// 	<see cref="P:Microsoft.WizardFramework.WizardPage.IsDataValid"/>
		/// Search for all the arguments that this Wizard gather
		/// If all of them are not null, then the Wizard can finish
		/// </summary>
		/// <value></value>
		public override bool IsDataValid
		{
			get
			{
				errorProvider.SetError(veCommandGuid, string.Empty);
				errorProvider.SetError(veCommandId, string.Empty);

				if(chkOther.Checked)
				{
					bool ret = true;

					if(DictionaryService.GetValue("OtherParentGroupGuid") == null)
					{
						errorProvider.SetError(veCommandGuid, Properties.Resources.InvalidValue);
						ret = false;
					}

					if(DictionaryService.GetValue("OtherParentGroupId") == null)
					{
						errorProvider.SetError(veCommandId, Properties.Resources.InvalidValue);
						ret = false;
					}

					if(!ret)
					{
						return false;
					}
				}
				else if(DictionaryService.GetValue("ParentGroupIdAlias") == null)
				{
					return false;
				}

				return base.IsDataValid;
			}
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
					this.veCommandCaption.BeginInit();
					this.veCommandCaption.ValueType = typeof(string);
					base.Site.Container.Add(veCommandCaption);
					this.veCommandCaption.EndInit();

					this.veParentGroupIdAlias.BeginInit();
					this.veParentGroupIdAlias.ValueType = typeof(string);
					base.Site.Container.Add(veParentGroupIdAlias);
					this.veParentGroupIdAlias.EditorInstance = new VSShellMenuIdEditor();
					this.veParentGroupIdAlias.ConverterInstance = new VSShellMenuIdConverter();
					this.veParentGroupIdAlias.EndInit();

					this.veCommandIcon.BeginInit();
					this.veCommandIcon.ValueType = typeof(string);
					base.Site.Container.Add(veCommandIcon);
					FileChooserEditor fileChooserEditor = new FileChooserEditor();
					StringDictionary editorAttributes = new StringDictionary();
					editorAttributes.Add("Title", Properties.Resources.BitmapChooserTitle);
					editorAttributes.Add("Filter", Properties.Resources.BitmapChooserFilter);
					fileChooserEditor.Configure(editorAttributes);
					this.veCommandIcon.EditorInstance = fileChooserEditor;
					this.veCommandIcon.EndInit();

					this.veCommandGuid.BeginInit();
					this.veCommandGuid.ValueType = typeof(string);
					base.Site.Container.Add(veCommandGuid);
					this.veCommandGuid.ConverterInstance = new CommandGuidConverter();
					this.veCommandGuid.EndInit();

					this.veCommandId.BeginInit();
					this.veCommandId.ValueType = typeof(string);
					base.Site.Container.Add(veCommandId);
					this.veCommandId.ConverterInstance = new CommandIdConverter();
					this.veCommandId.EndInit();
				}
			}
		}

		private void SetControls()
		{
			pctIconPreview.ImageLocation = string.Empty;
			lblCommandGuid.Visible = false;
			veCommandGuid.Visible = false;
			lblCommandId.Visible = false;
			veCommandId.Visible = false;
			pctCommandGuid.Visible = false;
		}

		private void veCommandCaption_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			string commandCaption = veCommandCaption.Value as string;
			DictionaryService.SetValue("CommandCaption", commandCaption);
		}

		private void veParentGroupIdAlias_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			string parentGroupIdAlias = veParentGroupIdAlias.Value as string;
			DictionaryService.SetValue("ParentGroupIdAlias", parentGroupIdAlias);
		}

		private void veCommandIcon_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			string bitmapFileName = (string)veCommandIcon.Value;
			DictionaryService.SetValue("BitmapFileName", bitmapFileName);
			pctIconPreview.ImageLocation = bitmapFileName;
		}

		private void pctCommandCaption_Click(object sender, EventArgs e)
		{
			Bitmap bitmap = CustomPageHelper.GetBitmapResource("CommandCaption");

			if(bitmap != null)
			{
				InfoForm infoForm = new InfoForm(bitmap);

				infoForm.ShowDialog();
			}
		}

		private void pctCommandGuid_Click(object sender, EventArgs e)
		{
			Bitmap bitmap = CustomPageHelper.GetBitmapResource("EnableVSIPLogging");

			if(bitmap != null)
			{
				InfoForm infoForm = new InfoForm(bitmap);

				infoForm.ShowDialog();
			}
		}

		private void chkOther_CheckedChanged(object sender, EventArgs e)
		{
			DictionaryService.SetValue("DefaultParentGroup", !chkOther.Checked);
			lblCommandGuid.Visible = chkOther.Checked;
			veCommandGuid.Visible = chkOther.Checked;
			lblCommandId.Visible = chkOther.Checked;
			veCommandId.Visible = chkOther.Checked;
			pctCommandGuid.Visible = chkOther.Checked;
		}

		private void veCommandGuid_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veCommandGuid.ConverterInstance != null)
			{
				errorProvider.SetError(veCommandGuid, string.Empty);

				if(veCommandGuid.Value != null)
				{
					DictionaryService.SetValue("OtherParentGroupGuid", ((Guid)veCommandGuid.Value).ToString("D").ToUpper(CultureInfo.CurrentCulture));
				}
				else
				{
					DictionaryService.SetValue("OtherParentGroupGuid", null);
					errorProvider.SetError(veCommandGuid, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("OtherParentGroupGuid", ((Guid)veCommandGuid.Value).ToString("D").ToUpper(CultureInfo.CurrentCulture));
			}
		}

		private void veCommandId_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veCommandId.ConverterInstance != null)
			{
				errorProvider.SetError(veCommandId, string.Empty);

				if(veCommandId.Value != null)
				{
					DictionaryService.SetValue("OtherParentGroupId", veCommandId.Value.ToString());
				}
				else
				{
					DictionaryService.SetValue("OtherParentGroupId", null);
					errorProvider.SetError(veCommandId, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("OtherParentGroupId", veCommandId.Value.ToString());
			}
		}
	}
}